%::PROVIDES=(
	name	=> ["NO_NET"],
	desc	=> ["Application fail-over on network loss support"],
	type	=> "request",
	code	=> \&::do_handle_nonet
);

sub ::do_handle_nonet {
my $args=shift;
my $cmdname=$args->{"_CMDNAME"};
my ($ch,@mapps,$capp,$cnet);
my $resp;

	if(!exists($args->{NET})) {
		logwarn("$cmdname request missing NET argument");
		return "MISSING_ARG";
	}
	$cnet=$args->{NET};
	logmsg("Entered handle_nonet [failed network is '$cnet'].");

	#################################################################
	# We have a remote connection to cluster netd so check out	#
	# to see if any applications may need to be failed over.	#
	#################################################################

	@mapps=();
	for $capp (sort(keys(%$appmap))) {
		next if !exists($appmap->{$capp}->{STATE});
		if($appmap->{$capp}->{STATE} eq "STARTED" &&
			$appmap->{$capp}->{NODE} eq hostname) {
			my @nl=netlist($capp);
			logmsg("Networks used by '$capp' : " . join(",",@nl));
			if(scalar(grep {/^$cnet$/} @nl)) {
				push @mapps,$capp;
				logmsg("  -> Application '$capp' must be failed over.");
			}
		}
	}

	if(!scalar(@mapps)) {
		logmsg("Network $cnet has failed locally - but no applications make use of it.");
		return "OK 0";
	}

	if($ch=fork()) {
		logmsg("Spawned process $ch to stop applications: ".join(",",@mapps));
		push @PIDLIST,$ch;
		return "OK ".scalar(@mapps);
	} else {

		$SIG{CHLD}='DEFAULT';
		#########################################################
		# Child process stops and starts the applications on	#
		# the remote node if possible.				#
		#########################################################
		my ($rnode_ip,$rnode_msg)=get_node_ssh_ip(timeout=>2,xml=>$xml);
		for $capp (@mapps) {
			logmsg("Running command '/sbin/cluster/clhaltapp -A $capp -F --verbose --logcmd --force'.");
			system("/sbin/cluster/clhaltapp -A $capp -F --verbose --logcmd --force 2>&1");
			$rc=$?>>8;
			if($rc) {
				logwarn("Unable to stop application $capp - fail-over failed!");
				next;
			}
			if(!defined($rnode_ip)) {
				logwarn("Application $capp not started on remote node - no SSH route!");
				next;
			}
			logmsg("Running command 'ssh $rnode_ip /sbin/cluster/clstartapp -A $capp -F --verbose --logcmd --force'.");
			system("ssh $rnode_ip /sbin/cluster/clstartapp -A $capp -F --verbose --logcmd --force 2>&1");
			$rc=$?>>8;
			if($rc) {
				logwarn("Unable to start application $capp on $OTHER_NODE_NAME!");
				next;
			}
			logmsg("Application $capp failed over successfully.");
		}
		exit(0);
	}
}

1;
